# 自定义模版标签

from django import template
from ..models import Post, Category, Tag


# 使用装饰器将函数装饰为一个模版标签
register = template.Library()


@register.simple_tag
def get_recent_posts(num=5):
    """获取最新的5篇文章"""
    return Post.objects.all()[:num]


@register.simple_tag
def archives():
    """归档日期"""
    return Post.objects.dates('created_time', 'month', order='DESC')


@register.simple_tag
def get_categories():
    """获取分类列表"""
    return Category.objects.all()


@register.simple_tag
def get_tags():
    """标签云"""
    return Tag.objects.all()